/**
 *  @author yinghui.zhang 货物安全卡
 */
 
Ext.ns("cn.hls.web.action.dict");
Array.prototype.remove = function(val) {
	var index = this.indexOf(val);
	if (index > -1) {
	this.splice(index, 1);
	}
	};
var safetyCardForm  = cn.hls.web.action.dict.safetyCardForm;
safetyCardForm  = function(){
	var uploadPanel,win;
	   return { 
	      init : function() {
	      this.initRender();
	      },
	      ajaxFileUpload:function(type){
	    	  var filepath =$("#pathFile").val();
	    		 var extStart = filepath.lastIndexOf(".");
	    	     var ext = filepath.substring(extStart, filepath.length).toUpperCase();
	    	     if (ext != ".PDF"&&ext != ".DOC"&&ext != ".DOCX"
	    	    	 &&ext != ".BMP" && ext != ".PNG" && ext != ".GIF" 
	    	    		 && ext != ".JPG" && ext != ".JPEG"){
	    	    	 Ext.Mytip.msg('提示','文件格式不正确,请允许上传后辍名为pdf、doc、docx、bmp、gif、png、jpg、jpeg的文件!');
	    	    	 return;
	    	     }else{
	    	    	 var size=document.getElementById("pathFile").files[0].size;
	    	    	 if(size>10*1024*1024){//最大不能超过10M
	    	    		// alert("上传文件超过规定大小!");
	    	    		 Ext.Mytip.msg('提示','上传文件超过规定大小!');
	    	    		 return;
	    	    	 }
	    	     }
	    	     $.ajaxFileUpload({  
	    	         url: cfg.cxt+'/upload/uploadFile.action', 
	    	         secureuri: false,  
	    	         fileElementId: 'pathFile',  
	    	         dataType: 'json', //这里修改为content  
	    	         success: function (data, status) {  
	    	           if(status=="success"){
	    	        	   //alert("文件传成功");
	    	        	   Ext.Mytip.msg('提示','文件传成功!');
	    	        	   var files,attachDiv;
	    	        	   if(type=='safetyCard'){
	    	        		   files=$("#hid_filePath").val();
	    	        		   attachDiv=$("#file_div");
	    	        		   console.log(files);
	    	        		   if(files==''){
	    	        			   $("#hid_filePath").val(data.info);
	    	        		   }else{
	    	        			   files+=','+data.info;
	    	        			   $("#hid_filePath").val(files);//重新设置值
	    	        		   }
	    	        	   }else{
	    	        		   files=$("#hid_fileInfoPath").val();
	    	        		   attachDiv=$("#file_infoPath_div");
	    	        		   if(files==''){
	    	        			   $("#hid_fileInfoPath").val(data.info);
	    	        		   }else{
	    	        			   files+=','+data.info;
	    	        			   $("#hid_fileInfoPath").val(files);//重新设置值
	    	        		   }
	    	        	   }
	    	        	   
	    	        	   var fileName=data.info.substring(data.info.indexOf("/")+1);
	    	        	   var pre_fileName=fileName.substring(0,fileName.indexOf("."));
	    	        	   attachDiv.append('<span id="sp_'+pre_fileName+'" style="padding-left:8px;padding-right:5px;">'+fileName+'</span><a id="a_'+pre_fileName+'" href="javascript:void(0);" onclick=safetyCardForm.delFile("'+fileName+'");>删除</a>'
	    	        			   			+'<input type="hidden" id="hid_'+pre_fileName+'" value="'+data.info+'"/>');
	    	        	   $("#pathFile").hide();
	    	        	   $("#pathFile").val('');//清空上传中的文件
	    	        	   $("#pathFile").hide();//隐藏上传组件
	    	        	   win.hide();
	    	        	   win=null;
	    	           }else{
	    	        	   Ext.Mytip.msg('提示','文件上传失败!');
	    	           }
	    	         },  
	    	                   
	    	         error: function (data, status, e) {  
	    	            //alert(e);  
	    	            Ext.Mytip.msg('提示','文件上传失败，原因:'+e);
	    	         }  
	    	       }  
	    	     )  
	      },
	      /**
	       * 删除文件
	       */
	      delFile:function(fileName){
	    	  var element=fileName.substring(0,fileName.indexOf("."));
	    	  Ext.Msg.confirm('请确认',
						'<span style="color:red"><b>提示：</b>确定删除吗？</span>',
						function(btn, text) {
							if (btn == 'yes') {
								var fileName=$("#hid_"+element).val();
								Ext.Ajax.request({
									url : cfg.cxt + '/upload/delFile.action',
									success : function(response) {
										var rt = Ext.decode(response.responseText);
										if (rt.status==200) {
											Ext.Mytip.msg('提示', '删除成功！');
											$("#sp_"+element).remove();
											$("#hid_"+element).remove();
											$("#a_"+element).remove();
											//更新hid_filePath的内容
											var files=$("#hid_filePath").val();
											if($.trim(files)!=''){
												var a=files.split(',');
												if(a.length>1){
													a.remove(fileName);
													$("#hid_filePath").val(a.join(','));
												}
											}
										} else {
											bangni.ajaxError(response);
										}
									},
									failure : bangni.ajaxError,
									params : {
										"fileName":fileName
									}
								});
							}
						});
	      },
	      createUploadWin:function(type){
	    	  if(!uploadPanel){
	    		  safetyCardForm.createUploadPanel();
	    	  }
	    	  if(!win){
	    		  win = new ExtWin({
	    			width : 300,
	  				height : 150,
	  				title : '<div class="panelTitleCss">货物安全卡照片上传</div>',
	    		  	items:[uploadPanel],
	    		  	buttons : [{
						text : '保存',
						id:"btn_Save",
						iconCls : 'acceptIcon',
						handler : function() {
							safetyCardForm.ajaxFileUpload(type);
						}
	    		  	},{
						text : '关闭',
						iconCls : 'deleteIcon',
						handler : function() {
							$("#pathFile").hide();
							win.hide();
							win=null;
						}
					}]
	    		  });
	    	  }
	    	  $("#pathFile").show();//显示上传组件
	    	  win.show();
	      },
	      createUploadPanel:function(){
	    	  uploadPanel=new Ext.Panel({
	    		  layout : 'fit',
	    		  items: [{
			        	padding : 5,
						bodyStyle:'overflow-y:auto;overflow-x:hidden;',
						contentEl : 'pathFile'
			        }]
	    	  });
	    	  return uploadPanel;
	      },
			
	      initRender: function(){
	     	
	      }
	   }
	}();
	Ext.onReady(safetyCardForm.init, safetyCardForm);
